package controller

import (
	"imooc.com/hello/args"
	"imooc.com/hello/service"
	"imooc.com/hello/util"
	"net/http"
)

var contactService service.ContactService

func AddFriend(writer http.ResponseWriter, request *http.Request)  {
	var contactArg args.ContactArg
	err := util.Bind(*request, &contactArg)
	if err != nil {
		util.RespFail(writer, err.Error())
		return
	}

	dstId, err := userService.LoadByMobile(contactArg.Mobile)
	if err != nil {
		util.RespFail(writer, err.Error())
		return
	}

	err = contactService.AddFriend(contactArg.UserId, dstId)
	if err != nil {
		util.RespFail(writer, err.Error())
		return
	}

	util.RespOk(writer, nil, "添加成功")
}

func LoadFriends(writer http.ResponseWriter, request *http.Request)  {
	var contactArg args.ContactArg
	err := util.Bind(*request, &contactArg)
	if err != nil {
		util.RespFail(writer, err.Error())
		return
	}

	friends, err := contactService.LoadFriends(contactArg.UserId)

	if err != nil {
		util.RespFail(writer, err.Error())
		return
	}

	util.RespOk(writer, friends, "")
}